/**
 * 
 */
package spring.webapp.anmeldesystem.controller;

import java.security.Principal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.webapp.anmeldesystem.entity.Admin;
import spring.webapp.anmeldesystem.entity.Fachbereich;
import spring.webapp.anmeldesystem.entity.Professor;
import spring.webapp.anmeldesystem.entity.Student;
import spring.webapp.anmeldesystem.entity.User;
import spring.webapp.anmeldesystem.service.AdminService;
import spring.webapp.anmeldesystem.service.FachbereichService;
import spring.webapp.anmeldesystem.service.ProfessorService;
import spring.webapp.anmeldesystem.service.StudentService;

/**
 * Controller for Mapping a Student, Professor or admin page with Informationen.
 * 
 * @author Songhua Cui
 * 
 */
@Controller
@RequestMapping("/")
public class UserPageController {

	@Autowired
	private StudentService studentService;
	@Autowired
	private ProfessorService professorService;
	@Autowired
	private AdminService adminService;
	@Autowired
	private FachbereichService fachbereichService;

	/**
	 * @param model
	 * @param principal
	 * @return
	 */
	@RequestMapping(value = "/studentHome")
	public String getStudentHomePage(ModelMap model, Principal principal) {
		// User user =
		// (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		Student student = studentService.getStudentInformationen(principal
				.getName());
		model.addAttribute("student", student);
		return "studentHome";
	}

	/**
	 * @param model
	 * @param principal
	 * @return
	 */
	@RequestMapping(value = "/studentInfo")
	public String getStudentInfoPage(ModelMap model, Principal principal) {
		Student student = studentService.getStudentInformationen(principal
				.getName());
		model.addAttribute("student", student);
		return "studentInfo";
	}

	/**
	 * @param model
	 * @param principal
	 * @return
	 */
	@RequestMapping(value = "/professorHome")
	public String getProfessorHomePage(ModelMap model, Principal principal) {
		Professor professor = professorService
				.getProfessorInformationen(principal.getName());
		model.addAttribute("professor", professor);
		return "professorHome";
	}

	/**
	 * @param model
	 * @param principal
	 * @return
	 */
	@RequestMapping(value = "/professorInfo")
	public String getProfessorInfoPage(ModelMap model, Principal principal) {
		Professor professor = professorService
				.getProfessorInformationen(principal.getName());
		model.addAttribute("professor", professor);
		return "professorInfo";
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/adminHome", method = RequestMethod.GET)
	public String getAdminHomePage(ModelMap model, Principal principal) {
		Admin admin = adminService.getAdminInformation(principal.getName());
		model.addAttribute("admin", admin);
		List<User> users = adminService.getAllUsers();
		model.addAttribute("users", users);
		List<Fachbereich> fachbereich = fachbereichService.getFachbereich();
		model.addAttribute("fachbereich", fachbereich);
		return "adminHome";
	}

	/**
	 * Add new user to system.
	 * 
	 * @param email
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
	public @ResponseBody
	String getUser(
			@RequestParam(value = "userEmail", required = true) String email,
			@RequestParam(value = "oldPassword", required = false) String oldPassword,
			@RequestParam(value = "newPassword", required = false) String newPassword) {
		String message = "";
		if (!StringUtils.isBlank(newPassword)) {
			message = adminService.updatePassword(email, oldPassword,
					newPassword);
		} else {
			message = "Kennwort darf nicht leer sein!";
		}
		return message;
	}
}
